//
//  TLTools.swift
//  TAleartView
//
//  Created by 唐基林 on 2025/1/15.
//

import UIKit
import Foundation

@MainActor public func getDeviceWidthAndHeight() -> (width: CGFloat, height: CGFloat) {
    let screenSize = UIScreen.main.bounds
    return (screenSize.width, screenSize.height)
}

@MainActor public func getSafeAreaInsets() -> UIEdgeInsets {
    if #available(iOS 15.0, *) {
        if let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
           let window = scene.windows.first {
            return window.safeAreaInsets
        }
    } else {
        if let window = UIApplication.shared.windows.first {
            return window.safeAreaInsets
        }
    }
    return .zero
}

public func TLog<T>(_ message: T, file: String = #file, function: String = #function,
              line: Int = #line) {
    #if DEBUG
    let fileName = (file as NSString).lastPathComponent
    print("\(fileName):\(line) \(function) | \(message)")
    #endif
}

extension String {
    func Theight(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat {
        let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
        let boundingBox = self.boundingRect(with: constraintRect,
                                            options: [.usesLineFragmentOrigin, .usesFontLeading],
                                            attributes: [.font: font],
                                            context: nil)
        return ceil(boundingBox.height)
    }
}

extension UIColor {
    /// 创建一个 UIColor 实例，使用 RGB 颜色和可选的透明度
    /// - Parameters:
    ///   - red: 红色分量（0-255）
    ///   - green: 绿色分量（0-255）
    ///   - blue: 蓝色分量（0-255）
    ///   - alpha: 透明度（0.0-1.0），默认值为 1.0
    convenience init(rgb red: Int, _ green: Int, _ blue: Int, alpha: CGFloat = 1.0) {
        let r = CGFloat(red) / 255.0
        let g = CGFloat(green) / 255.0
        let b = CGFloat(blue) / 255.0
        self.init(red: r, green: g, blue: b, alpha: alpha)
    }
}  
